import router from './../router/index.js'
import { compareVersion } from '@@/utils/common.js'
import {Request} from '@@/api/api.js'
import {QuickRequest} from '@@/api/quick'


export const initSdkEvents = ({ eventCenter, vueState }) => {
    const eventsMaps = {
        ad_show: adShow,
        ad_open_page: adOpenPage,
        ad_config_h5: adConfigH5,
        ad_back_req: adBackReq,
        ad_download_info: adDownloadInfo,
        ad_callback_h5: adCallbackH5,
        init,
        afterRegist,
    }
    Object.keys(eventsMaps).map((eventName) => {
        eventCenter.setEventCallback('sdkEvent', eventName, (res) => {
            eventsMaps[eventName](res, { vueState })
        })
    })
}

/**
 * @description: init：事件
 * @param {}
 * @return:void
 */
const init = (res, { vueState }) => {
    console.log('init回调', res)
    const { app, source, bfs } = res
    vueState.bundle = app.bundle
    vueState.did = bfs.did
    vueState.pageInfo.channel = source || ''
    //   通知sdk h5准备就绪
    window.SDKPostMessage({
        event: 'h5_ready',
    })
}

/**
 * @description: ad_open_page:Ad通知打开H5页面
 * @param {}
 * @return:void
 */
const adOpenPage = (res) => {
    const {
        data: { target },
    } = res
    const routerMap = {
        [target]: {
            name: target,
        },
        taskHistory: { name: 'record' },
        redeemHistory: { name: 'record', params: { routerActive: 2 } },
    }
    router.push(routerMap[target])
}

/**
 * @description: ad_config_h5:Ad在H5页面ready时提交配置信息
 * @param {}
 * @return:void
 */
const adConfigH5 = (res, { vueState }) => {
    const { version, statusBarHeight, tabBarHeight, lan, header, withdraw } = res.data
    Request.setBaseInfo(header)
    QuickRequest.setBaseInfo(header)

    vueState.pageInfo.header = header
    vueState.pageInfo.lang = lan || 'en'
    vueState.versionControl.version = version || ''
    vueState.versionControl.needUpdate = version
        ? compareVersion(version, '1.7.9')
        : true
    vueState.pageStyle.statusBarHeight = statusBarHeight
    vueState.pageStyle.tabBarHeight = tabBarHeight
    vueState.pageStyle.showWithdraw = withdraw === 'on'
    // 屏幕适配
    document.body.style.paddingTop = vueState.pageStyle.statusBarHeight + 'px'
    document.body.style.paddingBottom = vueState.pageStyle.tabBarHeight + 'px'
    vueState.getBaseInfo()
    vueState.pageStatus.afterRegist = true
    vueState.EventEmitter.emit('afterRegist')
    console.log('ad_config_h5事件：', res, vueState)
}

/**
 * @description: ad_back_req:物理返回键
 * @param {}
 * @return:void
 */
const adBackReq = () => {
    const {
        currentRoute: {
            value: { name, path },
        },
    } = router
    const state = name === 'me' || path === '/' ? 1 : 0
    if (!state) {
        router.back()
    }
    window.SDKPostMessage({
        event: 'h5_back_rsp',
        data: {
            state,
        },
    })
}

/**
 * @description: ad_download_info:Ad通知H5当前下载器相关信息
 * @param {}
 * @return:void
 */
const adDownloadInfo = (res, { vueState }) => {
    vueState.pageInfo.downloadNum = res.data['cnt_uninstall'] || 0
    console.log('ad_download_info',res,vueState);
}
/**
 * @description: ad_show：H5被展示通知
 * @param {}
 * @return:void
 */
const adShow = (res, { vueState }) => {
    vueState.getBaseInfo()
    window.trackEvent({ ev: 'showPage' })
}
/**
 * @description: ad_callback_h5：H5被展示通知
 * @param {}
 * @return:void
 */
const adCallbackH5 = (res, { vueState }) => {
    vueState.getBaseInfo()
}
/**
 * @description: afterRegist：完成注册消息后下发消息
 * @param {}
 * @return:void
 */
const afterRegist = (res, { vueState }) => {
    console.log('afterRegist回调')
    vueState.getBaseInfo()
    vueState.pageStatus.afterRegist = true
    vueState.EventEmitter.emit('afterRegist')
}
